package com.njupt.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class HttpsClientTmp
{
	 public static String post(String url, Map<String,String> params) throws KeyManagementException, NoSuchAlgorithmException, ClientProtocolException, IOException {
	        DefaultHttpClient httpclient = httpClientTrustingAllSSLCerts();
	        HttpPost httpPost = new HttpPost(url);
	        if(params != null){
	        	List pairs = new ArrayList();
	             Set keys = params.keySet();
	             for(Iterator i = keys.iterator(); i.hasNext();) {
	                  String key = (String)i.next();
	                  pairs.add(new BasicNameValuePair(key, params.get(key)));
	             }
	             UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, "UTF-8");
	             httpPost.setEntity(p_entity);
	        }
	        
	        HttpResponse response = httpclient.execute( httpPost );
	       
			InputStream is = response.getEntity().getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			StringBuilder builder = new StringBuilder();
			String line = null;
			while((line = br.readLine())!=null)
				builder.append(line);
			return builder.toString();
	    }

	    private static DefaultHttpClient httpClientTrustingAllSSLCerts() throws NoSuchAlgorithmException, KeyManagementException {
	        DefaultHttpClient httpclient = new DefaultHttpClient();

	        SSLContext sc = SSLContext.getInstance("SSL");
	        sc.init(null, getTrustingManager(), new java.security.SecureRandom());

	        SSLSocketFactory socketFactory = new SSLSocketFactory(sc);
	        Scheme sch = new Scheme("https", 443, socketFactory);
	        httpclient.getConnectionManager().getSchemeRegistry().register(sch);
	        return httpclient;
	    }

	    private static TrustManager[] getTrustingManager() {
	        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
	            @Override
	            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
	                return null;
	            }

	            @Override
	            public void checkClientTrusted(X509Certificate[] certs, String authType) {
	                // Do nothing
	            }

	            @Override
	            public void checkServerTrusted(X509Certificate[] certs, String authType) {
	                // Do nothing
	            }
	        } };
	        return trustAllCerts;
	    }

}
